package com.demo.jingxi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.demo.jingxi.dao.XRoleMenuMapper;
import com.demo.jingxi.pojo.Menu;
import com.demo.jingxi.dao.XMenuMapper;
import com.demo.jingxi.service.IXMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aoyuan
 * @since 2023-09-25
 */
@Service
public class XMenuServiceImpl extends ServiceImpl<XMenuMapper, Menu> implements IXMenuService {


    @Override
    public List<Menu> getAllMenu() {
        // 一级菜单
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Menu::getParentId,0);
        List<Menu> menuList = this.list(wrapper);
        // 子菜单
        setMenuChildren(menuList);
        return menuList;
    }

    public void setMenuChildren(List<Menu> menuIdList) {
        if(menuIdList != null) {
            for (Menu menu:menuIdList) {
                LambdaQueryWrapper<Menu> subWrapper = new LambdaQueryWrapper();
                subWrapper.eq(Menu::getParentId, menu.getMenuId());
                List<Menu> subMenuList = this.list(subWrapper);
                menu.setChildren(subMenuList);
                // 递归
                setMenuChildren(subMenuList);
            }
        }
    }
}
